//引入公共文件
import '@utils/init.js';

const defaultImages = require('../images/default.jpeg') //加载js图片

// 翻页数据
const pageData = {
        currentPage: 1, //当前页
        pageSize: 3, //每页显示条数
        total: 0, // 总条数
        pages: 0 //  总页数
    }
    // 模糊搜索数据
const searchData = {
    type: 'name',
    value: '',
}


//获取学生数据引入getData.js
import { getStudents } from '@js/getData.js';

renderStudentsTable();
//渲染学生列表
async function renderStudentsTable() {
    const { total, pages, result } = await getStudents({...pageData, ...searchData });
    pageData.total = total; //总条数
    pageData.pages = pages; //总页数
    const studentsHtml = result.map(item => `
    <tr>
                                <td>${item._id}</td>
                                <td>${item.name}</td>
                                <td>${item.age}</td>
                                <td>${item.gender}</td>
                                <td>${item.professionId ? item.professionId.name : 暂无}</td>
                                <td>${item.classesId ? item.classesId.name : 暂无}</td>
                                <td><img   width="50" src="${item.imagesPath || defaultImages}" alt="" /></td>
                                <td>
                                    <button class='delete' data-id=${item._id}>删除</button>
                                    <button type="button"  class='update' data-id=${item._id}>修改</button>
                                </td>
</tr>  `).join('');
    $('#studentsTb').html(studentsHtml);
    $('#currentPage').text(pageData.currentPage)
    $('#pages').text(pageData.pages)
    $('#sum').text(pageData.total)
}

//下一页
$('#nextPage').click(() => {
    if (pageData.currentPage < pageData.pages) {
        pageData.currentPage++;
        renderStudentsTable();
    }
});
//上一页
$('#prevPage').click(() => {
        if (pageData.currentPage >= 1) {
            pageData.currentPage--;
            renderStudentsTable();
        }

    })
    //首页
$('#homePage').click(() => {
        pageData.currentPage = 1;
        renderStudentsTable();
    })
    //尾页
$('#lastPage').click(() => {
    pageData.currentPage = pageData.pages;
    renderStudentsTable();
})

//切换数据change事件
$('#pageSize').change(function() {
    pageData.currentPage = 1 //每次显示多条数据都在第一页开始
    pageData.pageSize = $(this).val();
    $('#branches').text($(this).val())
    renderStudentsTable();
})


//删除学生
$('#studentsTb').on('click', '.delete', async(e) => {
    const _id = $(e.target).data('id');
    const res = await ajax({
        url: '/api/students/deleteStudents',
        type: "POST",
        data: {
            _id
        },
    })
    if (res.code) {
        alert('学生删除成功');
        renderStudentsTable();
    }

})

//模糊搜索
$('#searchValue').change(function() {
    searchData.type = $('#searchType').val();
    searchData.value = $('#searchValue').val();
    renderStudentsTable();
})

//获取修改学生数据
$('#studentsTb').on('click', '.update', function() {
    location.href = `./studentsUpdate.html?=${$(this).data('id')}`;
})